/*
 * ConnectionDialog.java
 *
 * Created on November 23, 2006, 7:03 PM
 */

package quizgame.common;

import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author  gsohtell
 */
public abstract class ConnectionDialog extends JDialog {
    
    /**
     *  Create a connectiondialog with an application name.
     *  @param parent   The parent JComponent
     *  @param modal    Set to true if dialog should be made modal
     *  @param application  The name of the application. eg. "Big"
     */
    public ConnectionDialog(JFrame parent, boolean modal, String application) {
        super(parent, modal);
        init(application);
    }
        
    /**
     *  Create a connectiondialog.
     *  @param parent   The parent JComponent
     *  @param modal    Set to true if dialog should be made modal
     */
    public ConnectionDialog(JFrame parent, boolean modal) {
        super(parent, modal);
        init(null);
    }
    
    private void init(String application) {
        initComponents();
        
        if(application != null) {
            welcomeLabel.setText(welcomeLabel.getText() + " - " + application);
            setTitle(application + " - " + getTitle());
            
            serverAddressField.setText(ClientConfig.getInstance().getHostname());
            if(serverAddressField.getText().isEmpty() == false) {
                usernameField.requestFocus();
            }
        }
        
        pack();
        setVisible(true);
    }
    
    private void showWarning(String message) {
        messagePanel.removeAll();
        messagePanel.setLayout(new BorderLayout());
        messagePanel.add(new JOptionPane(message, JOptionPane.WARNING_MESSAGE, JOptionPane.DEFAULT_OPTION, null, new Object[0]));
        pack();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        passwordField = new javax.swing.JPasswordField();
        serverAddressField = new javax.swing.JTextField();
        usernameField = new javax.swing.JTextField();
        cancelButton = new javax.swing.JButton();
        connectButton = new javax.swing.JButton();
        messagePanel = new javax.swing.JPanel();
        welcomeLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Connect to server");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                WindowClosePressed(evt);
            }
        });

        jLabel1.setText("Server host:");

        jLabel2.setText("Username:");

        jLabel3.setText("Password:");

        passwordField.setToolTipText("The password for this username");
        passwordField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                ConnectionDialog.this.keyReleased(evt);
            }
        });

        serverAddressField.setToolTipText("Enter hostname or IP address of server. Connects to localhost if left empty.");
        serverAddressField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                ConnectionDialog.this.keyReleased(evt);
            }
        });

        usernameField.setToolTipText("The username you would like to log in as.");
        usernameField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                ConnectionDialog.this.keyReleased(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.setToolTipText("Abort");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelPressed(evt);
            }
        });

        connectButton.setText("Connect");
        connectButton.setToolTipText("Connect to specified server");
        connectButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                connectPressed(evt);
            }
        });

        welcomeLabel.setText("Welcome to Iron Quiz");

        org.jdesktop.layout.GroupLayout messagePanelLayout = new org.jdesktop.layout.GroupLayout(messagePanel);
        messagePanel.setLayout(messagePanelLayout);
        messagePanelLayout.setHorizontalGroup(
            messagePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(welcomeLabel)
        );
        messagePanelLayout.setVerticalGroup(
            messagePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(welcomeLabel)
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(messagePanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 322, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel3)
                            .add(jLabel1)
                            .add(jLabel2))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(usernameField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 237, Short.MAX_VALUE)
                            .add(passwordField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 237, Short.MAX_VALUE)
                            .add(serverAddressField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 237, Short.MAX_VALUE)))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(connectButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(cancelButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(messagePanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(serverAddressField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(usernameField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(passwordField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(cancelButton)
                    .add(connectButton))
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void keyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_keyReleased
        if(evt.getKeyCode() == KeyEvent.VK_ENTER) {
            connectButton.doClick();
        }
    }//GEN-LAST:event_keyReleased

    private void WindowClosePressed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_WindowClosePressed
        onCancel();
    }//GEN-LAST:event_WindowClosePressed
    
    private void cancelPressed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelPressed
        onCancel();
    }//GEN-LAST:event_cancelPressed

    private void connectPressed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_connectPressed
        try {
            onConnect(serverAddressField.getText(), usernameField.getText(), new String(passwordField.getPassword()));
            ClientConfig.getInstance().setHostname(serverAddressField.getText());
        } catch (MalformedURLException e) {
            showWarning("The hostname entered is invalid.");
        } catch (RemoteException e) {
            showWarning("Failed connecting to the server: " + e.getMessage());
        } catch (IOException e) {
            showWarning(e.getMessage());
        } catch (NotBoundException e) {
            showWarning("The server does not offer the requested object.");
        }
    }//GEN-LAST:event_connectPressed
    
    /**
     * Invoked when dialog is canceled.
     */
    protected abstract void onCancel();
    
    /**
     * Invoked when the Connect button is pressed.
     * @param serverHost the server host entered
     * @param username the username entered
     * @param password the pasword entered
     */
    protected abstract void onConnect(String serverHost, String username, String password) throws IOException, MalformedURLException, NotBoundException, RemoteException;
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton connectButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel messagePanel;
    private javax.swing.JPasswordField passwordField;
    private javax.swing.JTextField serverAddressField;
    private javax.swing.JTextField usernameField;
    private javax.swing.JLabel welcomeLabel;
    // End of variables declaration//GEN-END:variables
}
